import { reactive } from 'vue'
import router from '../router'
import Cookies from 'js-cookie'

export enum UserRole {
  ADMIN = 'admin',
  PUNISHMENTADMIN = 'reward-punishment-admin',
  PUNISHMENTUSER = 'reward-punishment-user'
}

export default reactive<UserStore>({
  token: Cookies.get('token') || '',
  deptName: '',
  username: '',
  roles: [],
  menu: [],
  setDeptName(deptName: string) {
    this.deptName = deptName
  },
  setToken(token: string) {
    this.token = token
    Cookies.set('token', token)
  },
  setUserName(username: string) {
    this.username = username
  },
  setRoles(roles: UserRole[]) {
    this.roles = roles
  },
  setMenu(menu: RouteRecordRaw[]) {
    this.menu = menu
  },
  clear() {
    this.token = ''
    this.username = ''
    this.roles = []
    this.menu = []
    Cookies.remove('token')

    router.getRoutes().forEach((item) => {
      if (item.meta.auth) {
        router.removeRoute(item.name as string)
      }
    })
  }
})
